package com.xb.springtest.controller;

import cn.hutool.json.JSONUtil;
import com.xb.springtest.dto.ReturnBody;
import com.xb.springtest.service.UserService;
import com.xb.springtest.dto.vo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "用户相关的api", tags = {"用户"})
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //1.商品添加
    @ApiOperation(value="商品新增", notes = "添加商品信息")
    @PutMapping("add")
    public ReturnBody addCategory(@RequestBody User user) {
        userService.addCategory(user);
        return ReturnBody.success("新增成功！");
    }

    //2.商品修改
    @ApiOperation(value = "商品修改", notes = "修改数据库中某个的商品信息")
    @PostMapping("update")
    public ReturnBody updateCategory(@RequestBody User user) {
        int result = userService.updateCategory(user);
        if(result<=0) {
            return ReturnBody.fail("未查询到该用户！");
        }
        return ReturnBody.success("修改成功！");
    }

    //3.商品删除
    @ApiOperation(value = "根据id删除商品", notes = "商品删除")
    @DeleteMapping("/delete/{id}")
    public ReturnBody deleteCategory(@ApiParam(required = true, value = "用户id",allowableValues = "range[1,5]",example = "1") @PathVariable int id) {
        userService.deleteCategory(id);
        return ReturnBody.success("删除成功！");
    }

    //4.根据ID查询商品信息
    @GetMapping("/get/{id}")
    @ApiOperation(value = "根据id查询商品", notes = "查询数据库中某个的商品信息")
    public ReturnBody getCategory(@ApiParam(required = true, value = "用户id",example = "1") @PathVariable int id) {
        return ReturnBody.success("查询成功！", userService.getCategory(id));
    }


    //5.分页查询
    @GetMapping("/list")
    @ApiOperation(value="商品查询(分页)", notes = "查询数据库中某个的商品信息")
    private ReturnBody UserList(@ApiParam(required = false, value = "用户名称") @RequestParam("name") String name) {
        return ReturnBody.success("查询成功！", userService.UserList(name));
    }
}